package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Project;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class ProjectDao {
	DBHelp<Project> db = new DBHelp<Project>();
	
	public boolean save (Project p) {
		String sql = "INSERT INTO t_project (proId,proName,`describe`,createTime) VALUES (?,?,?,?)";
		return db.executeSQL(sql, p.getProId(),p.getProName(),p.getDescribe(),p.getCreateTime());
	}
	
	
	private class ProjectRowMapper implements RowMapper<Project> {

		public Project mapRow(ResultSet rs) throws SQLException {
			Project p = new Project();

			p.setProId(rs.getString("proId"));
			p.setProName(rs.getString("proName"));
			p.setDescribe(rs.getString("describe"));
			p.setCreateTime(rs.getLong("createTime"));
			return p;
		}
		
	}

	public List<Project> findByUserid(String userid) {
		String sql = "SELECT proId,proName,`describe`,createTime FROM t_project WHERE proId IN(SELECT t_project_proId FROM t_user_project WHERE t_user_id=?) ORDER BY createTime DESC";
		return db.executeQueryForList(sql, new ProjectRowMapper(),userid);
	}

	public boolean update(String proId, String proName, String describe) {
		String sql = "UPDATE t_project SET proName=?,`describe`=? WHERE proId=?";
		return db.executeSQL(sql, proName,describe,proId);
		
	}


	
}
